<?php
/**
 * 功能：card_info
 * 
 * 
 */
class InfoAction extends CommonAction {
    public function index(){
        layout('Layout/common'); //布局文件Tpl/Layout/common.html

        $login_id = $this->login_id; //当前登录用户ID
        
        //分组
        $_group = M('Group');        
        $where_group = "uid='{$login_id}'";
        $group_data = $_group->where($where_group)->select();
        $group_list = array();
        foreach ($group_data as $k=>$v) {
            $group_list[$v['gid']] = $v['group_name'];
        }        
        
        $opt = $this->_get("opt",'','0'); //0 全部 1 分组项
        $gid = $this->_get("gid",'trim'); //分组ID
        $p = $this->_get('p', '', '1'); //当前页

        ////排序字段 默认为主键名
        $_order = $this->_get("_order",'','id');
        $_sort = $this->_get("_sort",'','1');
        $_sort = ($_sort == 1) ? 'desc' : 'asc';
        //card_info_group.id
        $order = "" . $_order . " " . $_sort;

        $real_name = $this->_param('real_name', "trim", '');

        $this->page_num = $this->page_size;
        
        $where = " card_info_group.uid='{$login_id}' ";
        if(0 == $opt){
            //$where = ' and card_info_group.g_state=1';
        } else {
            //$where = "card_info_group.g_state=1 and card_info_group.gid = ". $gid;
            $where .= " and card_info_group.gid = ". $gid;
        }
        if($real_name != "") {
            $where .= " and card_info.real_name like '%{$real_name}%'";
        }

        $field = "card_info_group.id,card_info_group.gid,card_info_group.uid,card_info_group.g_state,card_info_group.update_time,card_info.*";

        $_model = M('info_group');
        $res = $_model->join('card_info ON card_info_group.mid = card_info.mid')->where($where)->field($field)->order($order)->page($p.','.$this->page_num)->select();
        $data_list = empty($res) ? array() : $res;
        //dump($data_list);

        import("ORG.Util.Page");// 导入分页类
        $count = $_model->where($where)->count();// 查询满足要求的总记录数
        $Page = new Page($count,$this->page_num);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        //列表排序显示
        $sortImg = $_sort; //排序图标
        $sortAlt = $_sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示
        $_sort = $_sort == 'desc' ? 0 : 1; //排序方式
        //模板赋值显示
        $this->assign('sort', $_sort);
        $this->assign('order', $_sort);
        $this->assign('sortImg', $sortImg);
        $this->assign('sortType', $sortAlt);
        
        $this->assign('opt',$opt);
        $this->assign('gid',$gid);
        $this->assign('data_list',$data_list);
        $this->assign('group_list',$group_list);
        $this->assign('real_name',$real_name);
        $this->display();
    }

    //插入页面
    function add() {
        $uid = $this->login_id;

        $_group = M('Group');        
        $where = "uid='{$uid}'";
        $group_list = $_group->where($where)->select();
        
        $this->assign('group_list',$group_list);
        $this->display();
    }

    //插入
    public function insert(){
        $name = $this->getActionName();
        $model = D($name);
        $uid = $model->getMemberId();
        if (false === $model->create()) {
            $this->error($model->getError());
        }
        //保存当前数据对象
        $mid = $model->add();
        if ($mid !== false) { //保存成功
            //插入关系表
            $_info_group = D('Info_group');
            $data['mid'] = $mid;
            $data['uid'] = $uid;
            $data['gid'] = 0;
            $data['g_state'] = 1;
            $data['update_time'] = time();
            if (false === $_info_group->create()) {
                $this->error($_info_group->getError());
            }
            $id = $_info_group->add($data);
            if ($mid === false){
                $this->error('关联添加失败!');
            }

            $this->success('新增成功!',"/info/index");
        } else {
            //失败提示
            $this->error('新增失败!');
        }
    }

    //编辑页面
    function edit() {
        $uid = $this->login_id; //登录用户ID

        $name = $this->getActionName();
        $model = M($name);
        //$id = $_REQUEST [$model->getPk()];
        $mid = $_REQUEST ['id'];
        $vo = $model->getByMid($mid);

        //所属组ID
        $_info_group = M('Info_group');
        $where_info = "mid='{$mid}'";
        $info_group = $_info_group->where($where_info)->find();

        //分组
        $_group = M('Group');        
        $where = "uid='{$uid}'";
        $group_list = $_group->where($where)->select();

        $this->assign('vo', $vo);
        $this->assign('info_group',$info_group);
        $this->assign('group_list',$group_list);
        $this->display();
    }

    //更新
    function update() {
        $mid = $this->_param('mid','',0);
        $u_gid = $this->_param('gid','',0);

        $name = $this->getActionName();
        $model = D($name);
        if (false === $model->create()) {
            $this->error($model->getError());
        }
        // 更新数据
        $list = $model->save();
        if (false !== $list) {
            //更新所属分组
            //所属组ID
            $_info_group = M('Info_group');
            $where_info = "mid='{$mid}'";
            $info_group = $_info_group->where($where_info)->find();
            //dump($info_group);exit;
            if(isset($info_group['gid'])){
                $gid = $info_group['gid'];
                $iid = $info_group['id'];//info_group表id

                if($u_gid != $gid){
                    //update
                    $where = "id = '{$iid}'";
                    $data['gid'] = $u_gid;
                    $up = $_info_group->where($where)->save($data);
                    if (false === $up) {
                        $this->success('所属分组更改失败!',cookie('_currentUrl_'));
                    }
                }
            }

            $this->success('编辑成功!',cookie('_currentUrl_'));
        } else {           
            $this->error('编辑失败!');
        }
    }

    //根据条件禁用/启用表数据: flag [1启用 0禁用]
    public function forbid(){
        $mid = $this->_get('id','',0);
        $flag = $this->_get('flag','',0);

        $options = "mid='{$mid}'";
        $field = "g_state";
        $value = ($flag == 1) ? 1 : 0;
        $msg = ($flag == 1) ? '启用' : '禁用';

        $model = M('Info_group');
        if(FALSE === $model->where($options)->setField($field,$value)){
            $this->error =  L('_OPERATION_WRONG_');
            //return false;
            $this->success($msg.'失败!');
        }else {
            //return True;
            $this->success($msg.'成功!',"/info/index");
        }
    }


    

    
}